/*
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/
#ifndef IPPARSER_H_
#define IPPARSER_H_

#include "Abstract.h"

typedef struct
{
	int ip[4];
}Ip_t;

class ipParser : public AbstractParser<Ip_t>
{
public:
	ipParser() {}
	virtual ~ipParser() {}
	bool Parse(char* str, int offset, Ip_t* val)
	{
		Ip_t adr;
		adr.ip[0] = adr.ip[1]= adr.ip[2]= adr.ip[3] = -1;
		sscanf(str + offset, "%d.%d.%d.%d", &adr.ip[0], &adr.ip[1], &adr.ip[2], &adr.ip[3]);
		for (int n =0; n < 4; n++) {
			
			if (adr.ip[n] < 0 || adr.ip[n] > 255) {
			
				DBGOUT << "invalid IP" << std::endl;
				return false;
			}
		}
		memcpy(val, &adr, sizeof(Ip_t));
		return true;
	}

	bool Parse(const char* str, int offset, Ip_t* val)
	{
		Ip_t adr;
		adr.ip[0] = adr.ip[1]= adr.ip[2]= adr.ip[3] = -1;
		sscanf(str + offset, "%d.%d.%d.%d", &adr.ip[0], &adr.ip[1], &adr.ip[2], &adr.ip[3]);
		for (int n =0; n < 4; n++) {
			
			if (adr.ip[n] < 0 || adr.ip[n] > 255) {
			
				DBGOUT << "invalid IP" << std::endl;
				return false;
			}
		}
		memcpy(val, &adr, sizeof(Ip_t));
		return true;
	}

};

#endif /*IPPARSER_H_*/
